package com.softer.wxzj.controller;

import com.github.pagehelper.PageInfo;
import com.softer.wxzj.common.aop.annotation.LogRecord;
import com.softer.wxzj.common.aop.enums.LogOperationType;
import com.softer.wxzj.common.aop.enums.LogOperationUnit;
import com.softer.wxzj.common.entity.CommonResult;
import com.softer.wxzj.common.entity.Page;
import com.softer.wxzj.common.enums.NumStrEnum;
import com.softer.wxzj.common.enums.NumberEnum;
import com.softer.wxzj.entity.AccCalculateInterestInfo;
import com.softer.wxzj.service.AccCalculateInterestInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * 结息配置
 * @author zcz
 * @date 2019/08/01
 */
@RestController
@RequestMapping("/accCalculateInterestInfo")
@Slf4j
public class AccCalculateInterestInfoController {
    @Autowired
    private AccCalculateInterestInfoService accCalculateInterestService;

    /**
     * 查询结息信息列表
     * @param page
     * @param accCalculateInterestInfo
     * @return
     */
    @RequestMapping("/getList")
    @LogRecord(description = "查询结息信息列表", operationUnit = LogOperationUnit.ACC_CALCULATE_INTEREST_INFO, operationType = LogOperationType.SELECT)
    public PageInfo<AccCalculateInterestInfo> getList(Page page, AccCalculateInterestInfo accCalculateInterestInfo) {
        return accCalculateInterestService.getList(page, accCalculateInterestInfo);
    }
    /**
     * 修改结息信息
     * @param accCalculateInterestInfo
     * @return
     */
    @RequestMapping("/update")
    @LogRecord(description = "修改结息信息", operationUnit = LogOperationUnit.ACC_CALCULATE_INTEREST_INFO, operationType = LogOperationType.UPDATE)
    public CommonResult update(AccCalculateInterestInfo accCalculateInterestInfo) {
        return accCalculateInterestService.update(accCalculateInterestInfo);
    }

    /**
     * 查询利率明细列表
     * @param id
     * @param fuzzy
     * @return
     */
    @RequestMapping("/getRateDetailList")
    @LogRecord(description = "查询利率明细列表", operationUnit = LogOperationUnit.ACC_CALCULATE_INTEREST_INFO, operationType = LogOperationType.SELECT)
    public PageInfo<AccCalculateInterestInfo> getRateDetailList(Page page, @RequestParam("id") String id, String fuzzy) {
        return accCalculateInterestService.getRateDetailList(page, id, fuzzy);
    }

    /**
     * 预结息
     * @param id
     * @return
     */
    @RequestMapping("/advanceInterest")
    @LogRecord(description = "预结息", operationUnit = LogOperationUnit.ACC_CALCULATE_INTEREST_INFO, operationType = LogOperationType.UPDATE)
    public CommonResult advanceInterest(@RequestParam("id") String id) {
        CommonResult commonResult = new CommonResult();
        try{
            //判断结息状态
            AccCalculateInterestInfo accCalculateInterestInfo = accCalculateInterestService.getInfo(id);
            Map<String, String> calculateMap = accCalculateInterestService.checkCalculate(accCalculateInterestInfo);
            if(NumStrEnum.ZERO.getNum().equals(calculateMap.get("code"))){
                return CommonResult.strResult(NumberEnum.ZERO.getNum(),calculateMap.get("msg"));
            }
            //修改状态为结息中
            accCalculateInterestService.modifyIsCalculation(id,NumStrEnum.ONE.getNum());
            commonResult =  accCalculateInterestService.advanceInterest(id);
        }catch (Exception e){
            log.info("预结息失败："+e.getMessage());
        }finally {
            //修改状态为不在结息中
            accCalculateInterestService.modifyIsCalculation(id,NumStrEnum.ZERO.getNum());
        }
        return commonResult;
    }

    /**
     * 结息信息汇总查询,小区列表
     * @param page
     * @param accCalculateInterestInfo
     * @return
     */
    @RequestMapping("/getInterestRegionList")
    @LogRecord(description = "结息信息汇总查询,小区列表", operationUnit = LogOperationUnit.ACC_CALCULATE_INTEREST_INFO, operationType = LogOperationType.SELECT)
    public PageInfo<AccCalculateInterestInfo> getInterestRegionList(Page page, AccCalculateInterestInfo accCalculateInterestInfo) {
        return accCalculateInterestService.getInterestRegionList(page, accCalculateInterestInfo);
    }

    /**
     * 结息信息汇总查询,自然幢列表
     * @param page
     * @param accCalculateInterestInfo
     * @return
     */
    @RequestMapping("/getInterestBuildList")
    @LogRecord(description = "结息信息汇总查询,自然幢列表", operationUnit = LogOperationUnit.ACC_CALCULATE_INTEREST_INFO, operationType = LogOperationType.SELECT)
    public PageInfo<AccCalculateInterestInfo> getInterestBuildList(Page page, AccCalculateInterestInfo accCalculateInterestInfo) {
        return accCalculateInterestService.getInterestBuildList(page, accCalculateInterestInfo);
    }

    /**
     * 结息信息汇总查询,房屋列表
     * @param page
     * @param accCalculateInterestInfo
     * @return
     */
    @RequestMapping("/getInterestHouseList")
    @LogRecord(description = "结息信息汇总查询,房屋列表", operationUnit = LogOperationUnit.ACC_CALCULATE_INTEREST_INFO, operationType = LogOperationType.SELECT)
    public PageInfo<AccCalculateInterestInfo> getInterestHouseList(Page page, AccCalculateInterestInfo accCalculateInterestInfo) {
        return accCalculateInterestService.getInterestHouseList(page, accCalculateInterestInfo);
    }

    /**
     * 结息信息汇总查询,房屋详情列表
     * @param page
     * @param accCalculateInterestInfo
     * @return
     */
    @RequestMapping("/getInterestHouseDetailList")
    @LogRecord(description = "结息信息汇总查询,房屋详情列表", operationUnit = LogOperationUnit.ACC_CALCULATE_INTEREST_INFO, operationType = LogOperationType.SELECT)
    public PageInfo<AccCalculateInterestInfo> getInterestHouseDetailList(Page page, AccCalculateInterestInfo accCalculateInterestInfo) {
        return accCalculateInterestService.getInterestHouseDetailList(page, accCalculateInterestInfo);
    }

    /**
     * 结息清册，小区列表
     * @param page
     * @param accCalculateInterestInfo
     * @return
     */
    @RequestMapping("/getInventoryRegionList")
    @LogRecord(description = "结息清册，小区列表", operationUnit = LogOperationUnit.ACC_CALCULATE_INTEREST_INFO, operationType = LogOperationType.SELECT)
    public PageInfo<AccCalculateInterestInfo> getInventoryRegionList(Page page, AccCalculateInterestInfo accCalculateInterestInfo) {
        return accCalculateInterestService.getInventoryRegionList(page, accCalculateInterestInfo);
    }
    /**
     * 结息清册，自然幢列表
     * @param page
     * @param accCalculateInterestInfo
     * @return
     */
    @RequestMapping("/getInventoryBuildList")
    @LogRecord(description = "结息清册，自然幢列表", operationUnit = LogOperationUnit.ACC_CALCULATE_INTEREST_INFO, operationType = LogOperationType.SELECT)
    public PageInfo<AccCalculateInterestInfo> getInventoryBuildList(Page page, AccCalculateInterestInfo accCalculateInterestInfo) {
        return accCalculateInterestService.getInventoryBuildList(page, accCalculateInterestInfo);
    }
    /**
     * 结息清册，房屋列表
     * @param page
     * @param accCalculateInterestInfo
     * @return
     */
    @RequestMapping("/getInventoryHouseList")
    @LogRecord(description = "结息清册，房屋列表", operationUnit = LogOperationUnit.ACC_CALCULATE_INTEREST_INFO, operationType = LogOperationType.SELECT)
    public PageInfo<AccCalculateInterestInfo> getInventoryHouseList(Page page, AccCalculateInterestInfo accCalculateInterestInfo) {
        return accCalculateInterestService.getInventoryHouseList(page, accCalculateInterestInfo);
    }

    /**
     * 结息 确定资金结息，并写入财务
     * @param id 结息记录 id
     * @return
     */
    @RequestMapping("/calculateInterest")
    @LogRecord(description = "结息", operationUnit = LogOperationUnit.ACC_CALCULATE_INTEREST_INFO, operationType = LogOperationType.UPDATE)
    public CommonResult calculateInterest(@RequestParam("id") String id){
        CommonResult commonResult = new CommonResult();
        try{
            //判断结息状态
            AccCalculateInterestInfo accCalculateInterestInfo = accCalculateInterestService.getInfo(id);
            Map<String, String> calculateMap = accCalculateInterestService.checkCalculate(accCalculateInterestInfo);
            if(NumStrEnum.ZERO.getNum().equals(calculateMap.get("code"))){
                return CommonResult.strResult(NumberEnum.ZERO.getNum(),calculateMap.get("msg"));
            }
            //修改状态为结息中
            accCalculateInterestService.modifyIsCalculation(id,NumStrEnum.ONE.getNum());
            commonResult = accCalculateInterestService.calculateInterest(id);
        }catch (Exception e){
            log.info("结息失败："+e.getMessage());
        }finally {
            //修改状态为不在结息中
            accCalculateInterestService.modifyIsCalculation(id,NumStrEnum.ZERO.getNum());
        }
        return commonResult;
    }

    /**
     * 按预结息写入财务
     * @param id 结息记录 id
     * @return
     */
    @RequestMapping("/accordAdvanceInterest")
    @LogRecord(description = "预结息转结息", operationUnit = LogOperationUnit.ACC_CALCULATE_INTEREST_INFO, operationType = LogOperationType.UPDATE)
    public CommonResult accordAdvanceInterest(@RequestParam("id") String id){
        CommonResult commonResult = new CommonResult();
        try{
            //判断结息状态
            AccCalculateInterestInfo accCalculateInterestInfo = accCalculateInterestService.getInfo(id);
            Map<String, String> calculateMap = accCalculateInterestService.checkCalculate(accCalculateInterestInfo);
            if(NumStrEnum.ZERO.getNum().equals(calculateMap.get("code"))){
                return CommonResult.strResult(NumberEnum.ZERO.getNum(),calculateMap.get("msg"));
            }
            //修改状态为结息中
            accCalculateInterestService.modifyIsCalculation(id, NumStrEnum.ONE.getNum());
            commonResult = accCalculateInterestService.accordAdvanceInterest(id);
        }catch (Exception e){
            log.info("结息失败："+e.getMessage());
        }finally {
            //修改状态为不在结息中
            accCalculateInterestService.modifyIsCalculation(id,NumStrEnum.ZERO.getNum());
        }
        return commonResult;
    }

    /**
     * 查询是否预结息  1已结息  0未结息
     * @param id
     * @return
     */
    @RequestMapping("/getIsAdvanceById")
    @LogRecord(description = "查询是否预结息", operationUnit = LogOperationUnit.ACC_CALCULATE_INTEREST_INFO, operationType = LogOperationType.UPDATE)
    public String getIsAdvanceById(@RequestParam("id") String id){
        return accCalculateInterestService.getIsAdvanceById(id);
    }
    @RequestMapping("/sendFinance")
    @LogRecord(description = "查询是否预结息", operationUnit = LogOperationUnit.ACC_CALCULATE_INTEREST_INFO, operationType = LogOperationType.UPDATE)
    public String sendFinance(String rateNo){
        accCalculateInterestService.sendFinance(rateNo);
        return "chenggong";
    }

}
